package ext.acae2.baseline.ui;

import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import wt.util.WTException;
import wt.util.WTMessage;
import wt.vc.baseline.ManagedBaseline;

import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationFeedbackMsg;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.acae2.baseline.BaselineConstants;
import ext.acae2.baseline.helper.BaselineHelper;
import ext.acae2.baseline.resource.BaselineActionResource;

/**
 * @desription TODO
 * @author cnie
 * @date 2013-3-28 下午3:56:18
 */
public class BaselineExitValidator extends DefaultUIComponentValidator{
	
	private static final String RESOURCE = BaselineActionResource.class.getName();
	
	static final String SPILT_STR = "-";
	
	static final int seq =3;
	static final String defaultValue="0";
	
	static String FIRST_NUMBER = "001";

	@Override
	public UIValidationResult validateFormSubmission(UIValidationKey uiValidationKey,
			UIValidationCriteria uivalidationCriteria, Locale local) throws WTException {
		
		String projectCode =null;
		String phaseType =null;
		String baselineType =null;
		String createType = null;
		Map map = uivalidationCriteria.getParameterMap();
		for(java.util.Iterator it = map.keySet().iterator();it.hasNext();){
			String key = (String) it.next();
			if(key.endsWith("___combobox")&&key.indexOf(BaselineConstants.IBA_PROJECTCODE)>-1){
				String[] projectCodeArr = (String[]) map.get(key);
				projectCode = projectCodeArr[0];
			}
			if(key.endsWith("___combobox")&&key.indexOf(BaselineConstants.IBA_PHASETYPE)>-1){
				String[] phaseTypeArr = (String[]) map.get(key);
				if(phaseTypeArr!=null){
					phaseType = phaseTypeArr[0];
				}
				
			}
			if(key.endsWith("___combobox")&&key.indexOf(BaselineConstants.IBA_BASELINETYPE)>-1){
				String[] baselineTypeArr = (String[]) map.get(key);
				if(baselineTypeArr!=null){
					baselineType = baselineTypeArr[0];
				}
			}
			if(key.endsWith("createType___combobox")){
				String[] createTypeArr= (String[]) map.get(key);
				createType = createTypeArr[0];
			}
		}
		String number =null;
		StringBuffer nextNumber = new StringBuffer();
		
		if(createType.indexOf(BaselineConstants.TYPE_CONFIGBASELINE)>-1){
			
			int spiltIndex = projectCode.indexOf(SPILT_STR);
			number = String.valueOf(projectCode.charAt(0))+ String.valueOf(projectCode.charAt(spiltIndex + 1))
					+ String.valueOf(projectCode.charAt(projectCode.length() - 1))+"B"+SPILT_STR+defaultValue+baselineType.substring(0,1)+SPILT_STR;
		}else if(createType.indexOf(BaselineConstants.TYPE_PHASEBASELINE)>-1){
			int spiltIndex = projectCode.indexOf(SPILT_STR);
			number = String.valueOf(projectCode.charAt(0))+ String.valueOf(projectCode.charAt(spiltIndex + 1))
					+ String.valueOf(projectCode.charAt(projectCode.length() - 1))+"B"+SPILT_STR+"1"+phaseType.substring(0,1)+SPILT_STR;
		}else{
			return super.validateFormSubmission(uiValidationKey, uivalidationCriteria, local);
		}
		
		String maxNumber = BaselineHelper.searchBaselineLikeNumber(number);
		if (StringUtils.isBlank(maxNumber)) {
			nextNumber.append(number + FIRST_NUMBER);
		} else {
			nextNumber.append(number);
			StringBuffer liushuiSb = new StringBuffer();
			String temp = maxNumber.substring(maxNumber
					.length() - seq);
			int lastNum = Integer.parseInt(temp) + 1;
			for (int i = 0; i < seq; i++) {
				liushuiSb.append(defaultValue);
			}
			liushuiSb.append(lastNum);
			String liushuiStr = liushuiSb.toString().substring(
					liushuiSb.toString().length() - seq);
			nextNumber.append(liushuiStr);
		}
		
		ManagedBaseline exitbaseline = BaselineHelper.getBaseLineByNumber(nextNumber.toString());
		UIValidationResult uivalidationresult = UIValidationResult.newInstance(uiValidationKey, UIValidationStatus.PERMITTED);
		if(exitbaseline!=null){
			uivalidationresult.setStatus(UIValidationStatus.DENIED);
			String createError = WTMessage.getLocalizedMessage(RESOURCE, BaselineActionResource.MESSAGE_BASELINE_CREATE_ERROR,null,local);
			uivalidationresult.addFeedbackMsg(UIValidationFeedbackMsg.newInstance(createError, FeedbackType.ERROR));
			return uivalidationresult;
		}else{
			// TODO Auto-generated method stub
			return super.validateFormSubmission(uiValidationKey, uivalidationCriteria, local);
		}
		
		
	}
	
	

}
